﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Threading;
using System.Windows.Forms;
using DevExpress.Utils;
using DevExpress.XtraEditors;

namespace TaxiDispatchClient
{
    public partial class TaxiWarmingHistoryForm : DevExpress.XtraEditors.XtraForm
    {
        public TaxiWarmingHistoryForm()
        {
            InitializeComponent();
        }

        public TaxiWarmingHistoryForm(DataRow row)
        {
            InitializeComponent();
            _taxiNo = row["出租车牌照"].ToString();
            btnEdt_phoneNumber.Text = row["司机号码"].ToString();          
            //CreatDatatable();
            //gridView1.GroupPanelText = "出租车报警记录";            
            //gridControl1.DataSource = _dt;            
        }
        private void TaxiWarmingHistoryForm_Load(object sender, EventArgs e)
        {
            dateEdit_start.DateTime = DateTime.Now.AddDays(-7);
            dateEdit_end.DateTime = DateTime.Now;
            _currentPage = 1;
            _isLoad = true;
            _thread = new Thread(BindGrid);
            _thread.Start();
        }

        private DataTable _dt;
        private string _taxiNo;
        private int _currentPage;
        private int _pageCount;
        private int _pageSize =20;
        private int _recordCount;
        private bool _isLoad;
        private bool _isSelect;
        private Thread _thread;
      //  private string _title;
   
        private void CreatDatatable()
        {
            _dt = new DataTable();            
            _dt.Columns.Add("司机手机号码");
            _dt.Columns.Add("出租车牌照");            
            _dt.Columns.Add("报警类型");
            _dt.Columns.Add("报警时间");            

            DataRow row = _dt.NewRow();
            row["司机手机号码"] = btnEdt_phoneNumber.Text;
            row["出租车牌照"] = _taxiNo;
            row["报警类型"] = "超速报警";
            row["报警时间"] = DateTime.Now.AddHours(-15).AddSeconds(-43);
            _dt.Rows.Add(row);

            row = _dt.NewRow();
            row["司机手机号码"] = btnEdt_phoneNumber.Text;
            row["出租车牌照"] = _taxiNo;
            row["报警类型"] = "疲劳驾驶";
            row["报警时间"] = DateTime.Now.AddHours(-55).AddSeconds(-57);
            _dt.Rows.Add(row);

            row = _dt.NewRow();
            row["司机手机号码"] = btnEdt_phoneNumber.Text;
            row["出租车牌照"] = _taxiNo;
            row["报警类型"] = "超速报警";
            row["报警时间"] = DateTime.Now.AddHours(-44).AddSeconds(-7);
            _dt.Rows.Add(row);

            row = _dt.NewRow();
            row["司机手机号码"] = btnEdt_phoneNumber.Text;
            row["出租车牌照"] = _taxiNo;
            row["报警类型"] = "超速报警";
            row["报警时间"] = DateTime.Now.AddHours(-75).AddSeconds(-26);
            _dt.Rows.Add(row);

            row = _dt.NewRow();
            row["司机手机号码"] = btnEdt_phoneNumber.Text;
            row["出租车牌照"] = _taxiNo;
            row["报警类型"] = "疲劳驾驶";
            row["报警时间"] = DateTime.Now.AddHours(-55).AddSeconds(-19); 
            _dt.Rows.Add(row);

            row = _dt.NewRow();
            row["司机手机号码"] = btnEdt_phoneNumber.Text;
            row["出租车牌照"] = _taxiNo;
            row["报警类型"] = "超速报警";
            row["报警时间"] = DateTime.Now.AddHours(-88).AddSeconds(-10);
            _dt.Rows.Add(row);
            
        }

        private void btn_select_Click(object sender, EventArgs e)
        {
           if(dateEdit_start.DateTime>dateEdit_end.DateTime)
           {
               XtraMessageBox.Show("截止时间不能大于开始时间！");
               dateEdit_end.Focus();
               return;               
           }
            _isSelect = true;
            _thread = new Thread(BindGrid);
            _thread.Start();
        }


        private void BindGrid()
        {
            string driver = btnEdt_phoneNumber.Text;
            int value = int.Parse(imgComBox_warmingType.Properties.Items[
                imgComBox_warmingType.SelectedIndex].Value.ToString());
            string state = "";          
            string timeStart = dateEdit_start.DateTime.ToString("yyyy-MM-dd HH:mm:ss");
            string timeEnd = dateEdit_end.DateTime.ToString("yyyy-MM-dd HH:mm:ss");
            //_title = dateEdit_start.DateTime.ToString("MM月dd号HH点")+
            //    dateEdit_end.DateTime.ToString("MM月dd号HH点");
            StringBuilder sql = new StringBuilder();
            if(driver !="")
            {
                sql.Append("PhoneNumber='");
                sql.Append(driver);
                sql.Append("' and ");
               // _title += "手机号为"+driver+"的出租车";
            }
            //_title += "报警记录";
            if(value!=-1)
            {
                state = Common.OperateAgreement.GetTaxiWarmingState(value);
                sql.Append(" Warmingstate='");
                sql.Append(state);
                sql.Append("' and ");
            }            
            sql.Append(" WarmingTime between '");
            sql.Append(timeStart);
            sql.Append("' and '");
            sql.Append(timeEnd);
            sql.Append("'");

            if (MainForm.MyUserInfo.CompanyName!=Service.PowerConfig.MainCompanyName)
            {
                sql.Append(" and CompanyName='");
                sql.Append(MainForm.MyUserInfo.CompanyName);
                sql.Append("'");
            }

            try
            {
                DataSet ds = MainForm.PositionService.TaxiWarmingGetListByPage(
                    _currentPage,_pageSize,sql.ToString());
                BeginInvoke(new MethodInvoker(() =>
                {
                    gridControl1.DataSource = ds.Tables[0];                    
                }));
                if(ds!=null)
                {
                    _recordCount = ds.Tables[0].Rows.Count;
                }
                if(_isLoad)
                {
                  //  SetGridStyle();                    
                }
                if(_isLoad||_isSelect)
                {
                    _recordCount = MainForm.PositionService.TaxiWarmingGetRecordCount(
                        sql.ToString());
                    _pageCount = int.Parse(Math.Ceiling((double)
                       _recordCount / _pageSize).ToString());

                    BeginInvoke(new MethodInvoker(() =>
                    {
                        gridView1.GroupPanelText = "共计" + _recordCount + "条";
                        label_totalPage.Text = _pageCount.ToString();
                        //label_currentPage.Text = _pageCount > 0 ? "1" : "0";                                              
                        if (_pageCount > 0)
                        {
                            label_currentPage.Text = "1";                         
                        }
                        else
                        {
                            label_currentPage.Text = "0";
                        }
                    }));
                    _isLoad = false;
                    _isSelect = false;
                }
                BeginInvoke(new MethodInvoker(() =>
                {
                    btn_prev.Enabled = true;
                    btn_next.Enabled = true;
                    btn_select.Enabled = true;                    
                }));
            }
            catch (Exception ex)
            {
                if (!ex.Message.Contains("正在中止线程"))
                {
                    Common.CommonRoutine.HandlingExceptions(ex);
                }
                else
                {
                    Common.CommonRoutine.HandlingExceptions(ex, true);
                    btn_prev.Enabled = true;
                    btn_next.Enabled = true;
                    btn_select.Enabled = true;                    
                }            
            }   
        }

        //private void SetGridStyle()
        //{
        //    BeginInvoke(new MethodInvoker(() =>
        //    {
        //        gridView1.GroupPanelText = "共计" + _recordCount + "条";
        //        gridView1.RowHeight = 21;
        //        gridView1.OptionsBehavior.Editable = false;
        //        gridView1.Columns["Id"].Visible = false;
        //        gridView1.Columns["PhoneNumber"].Caption = "司机电话";
        //        gridView1.Columns["PhoneNumber"].AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
        //        gridView1.Columns["PhoneNumber"].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
        //        gridView1.Columns["Warmingstate"].Caption = "告警状态";
        //        gridView1.Columns["Warmingstate"].AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
        //        gridView1.Columns["Warmingstate"].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;
        //        gridView1.Columns["WarmingTime"].Caption = "告警时间";
        //        gridView1.Columns["WarmingTime"].AppearanceHeader.TextOptions.HAlignment = HorzAlignment.Center;
        //        gridView1.Columns["WarmingTime"].AppearanceCell.TextOptions.HAlignment = HorzAlignment.Center;              
        //        gridView1.Columns["WarmingTime"].DisplayFormat.FormatType = FormatType.DateTime;
        //        gridView1.Columns["WarmingTime"].DisplayFormat.FormatString = "yyyy-MM-dd HH:mm:ss";
        //    }));
        //}


        // 上一页
        private void btn_prev_Click(object sender, EventArgs e)
        {
            if (_currentPage > 1)
            {
                _currentPage--;
                btn_prev.Enabled = false;
                btn_next.Enabled = false;
                btn_select.Enabled = false;        
                _thread = new Thread(BindGrid);
                _thread.Start();
                label_currentPage.Text = _currentPage.ToString();                
            }
        }

        //下一页
        private void btn_next_Click(object sender, EventArgs e)
        {
            if (_currentPage < _pageCount)
            {
                _currentPage++;
                btn_prev.Enabled = false;
                btn_next.Enabled = false;
                btn_select.Enabled = false;                
                _thread = new Thread(BindGrid);
                _thread.Start();
                label_currentPage.Text = _currentPage.ToString();                
            }
        }


        #region 窗体控件效果
        Point _mouseOff;
        bool _leftFlag;
        private void Form_MouseDown(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Left)
            {
                _mouseOff = new Point(-e.X, -e.Y);
                _leftFlag = true;
            }
        }

        private void Form_MouseMove(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                Point mouseSet = Control.MousePosition;
                mouseSet.Offset(_mouseOff.X, _mouseOff.Y);
                Location = mouseSet;
            }
        }

        private void Form_MouseUp(object sender, MouseEventArgs e)
        {
            if (_leftFlag)
            {
                _leftFlag = false;
            }
        }

        private void pictureEdit_exit_MouseHover(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexit;
            Cursor = Cursors.Hand;
        }

        private void pictureEdit_exit_MouseLeave(object sender, EventArgs e)
        {
            pictureBox1.Image = Properties.Resources.snedSmsexitPressDown;

            Cursor = Cursors.Arrow;

        }
        private void btn_MouseHover(object sender, EventArgs e)
        {
            Cursor = Cursors.Hand;
        }

        private void btn_MouseLeave(object sender, EventArgs e)
        {
            Cursor = Cursors.Arrow;
        }

        private void pictureEdit_exit_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void PhoneUserForm_MouseClick(object sender, MouseEventArgs e)
        {
            if (e.Button == MouseButtons.Right)
            {
                Close();
            }
        }
        #endregion

        private void btn_exportToExcel_Click(object sender, EventArgs e)
        {
            //saveFileDialog1.FileName = "手机用户列表";
            saveFileDialog1.FileName ="出租车报警记录";
            saveFileDialog1.Filter = "Excel文件(*.xls)|*.xls";
            if (saveFileDialog1.ShowDialog() == DialogResult.OK)
            {
                gridView1.ExportToXls(saveFileDialog1.FileName);
                XtraMessageBox.Show("Excel导出完成！");
            }
        }

        private void btnEdt_phoneNumber_ButtonClick(object sender, DevExpress.XtraEditors.Controls.ButtonPressedEventArgs e)
        {
            DriverInfoForm driverInfoForm = new DriverInfoForm();
            driverInfoForm.ShowDialog();
            if (driverInfoForm.gridView1.FocusedRowHandle >= 0)
            {               
                DataRow row = driverInfoForm.gridView1.GetDataRow(driverInfoForm.gridView1.FocusedRowHandle);
                btnEdt_phoneNumber.Text = row["PhoneNumber"].ToString();                
            } 
        }

      

    }
}